package com.xiaodou.mjf.web.console.core.controller;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.xiaodou.mjf.application.core.manager.FinancingManager;
import com.xiaodou.mjf.application.core.service.FinancialPackageService;
import com.xiaodou.mjf.application.core.service.FinancingAttributeService;
import com.xiaodou.mjf.application.core.service.FinancingService;
import com.xiaodou.mjf.application.core.service.FinancingStatisticsService;
import com.xiaodou.mjf.application.core.service.ProductService;
import com.xiaodou.mjf.entity.core.FinancialPackage;
import com.xiaodou.mjf.entity.core.Financing;
import com.xiaodou.mjf.entity.core.Product;
import com.xiaodou.mjf.infrastructure.common.controller.ApplicationController;

/**
 * 小豆盒子Controller
 * @author BILLY
 *
 */
@Controller
@RequestMapping("financing/staging")
public class StagingProductController extends  ApplicationController {

	@Resource FinancingService financingService;
	@Resource FinancingStatisticsService financingStatisticsService;
	@Resource FinancingAttributeService financingAttributeService;
	@Resource FinancingManager financingManager;
	@Resource FinancialPackageService financialPackageService;
	@Resource ProductService productService;
	
	@RequestMapping("")
	public String staging(FinancialPackage financialPackage ){
	
		return "core/finance/staging/list";
	}

    @RequestMapping(value = {"data.json"})
    @ResponseBody
    public Map<String,Object> data(Product product){
        Page<Product> page = productService.pageAll(product);
        return page(page);
    }
    
    @RequestMapping(value={"interestRate","interestRate/{id}"})
    @ResponseBody
    public boolean checkInterestRate(@PathVariable String id, Integer term){
    	boolean flag = false;
 	   try{
 		   Financing financing = new Financing();
 		   financing = financingService.getFinancingByTermAndInvsetStatus(term, Financing.INVEST_STATUS_SALE,Financing.TYPE_XIAODOU);
 		   if(financing==null) {
 			  flag = true; 
 		   }
 	   }catch(Exception ex){
 		   logger.error(ex.toString());
 		   throw ex;
 	   }
 	   return flag;
    }
    
	@RequestMapping(value={"form","form/{id}"},method=RequestMethod.GET)
	public String form(Model model,@PathVariable String id){
		try{
			FinancialPackage financialPackage = financialPackageService.get(id);
			model.addAttribute("financialPackage", financialPackage);
//			List<FinancialPackage> lists = financialPackageService.list();
//			model.addAttribute("lists", lists);
		}catch(Exception ex){
			logger.error(ex.toString());
			throw ex;
		}
		
		return "core/finance/staging/form";
	}
	
	@RequestMapping(value={"save"},method=RequestMethod.POST)
	public String save(FinancialPackage financialPackage,MultipartFile file,Model model) throws Exception{
		try{
			
//			financing.setType(Financing.TYPE_MEILI);	
//			financingService.save(financing);
//			
//			String path = FileUtils.getPath(Financing.IMAGE_PATH,financing.getId());
//	        String fileName = FileUtils.getFileName(file.getOriginalFilename());			
//			FileUtils.upload(Global.getFtpHost(), Global.getFtpUser(), Global.getFtpPassword(), Global.getFtpPort(), file.getInputStream(), path, fileName);
//			financing.setIcon(path + "/" + fileName);			
//			financingService.save(financing);
			financialPackageService.save(financialPackage);
			
		}catch(Exception ex){
			logger.error(ex.toString());
			ex.printStackTrace();
			throw ex;
		}		
		return "redirect:";
	}
	
	
}
